package com.codermn.servlet;

/**
 * @author jxau_scf
 * @date 2021/5/13 - 20:53
 */

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.*;
import javax.servlet.http.*;

//@WebServlet(urlPatterns = "/CharServlet.do")
public class CharServlet extends HttpServlet {

    @Override
    public void init(ServletConfig config) throws ServletException {
        System.out.println("Servlet初始化结束");
    }

    // 表示处理get请求
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        System.out.println("** Servlet doGet处理提交参数 ...");
        this.doPost(req, resp);
    }

    // 处理post请求
    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        HttpSession session = req.getSession();
        String name = req.getParameter("name");
        String upass = req.getParameter("pass");
        if (((("zs".equals(name)) && ("123".equals(upass)))
                || (("ls".equals(name)) && ("123".equals(upass))))) {

            session.setAttribute("uname", name);
            System.out.println("添加session,name为"+name);
//            Set alluser = (Set)req.getAttribute("alluser");
//            System.out.println(alluser.toString());

            Set alluser = (Set)this.getServletContext().getAttribute("alluser");
            alluser.add(name);
            System.out.println("servlet在set集合中添加用户"+name);
            System.out.println(alluser.toString());
//            Map allmap = (Map)this.getServletContext().getAttribute("allmap");
//            System.out.println(allmap.toString());
//
            req.setAttribute("alluser", alluser);

            req.getRequestDispatcher("showalluser.jsp").forward(req, resp);
        } else {
            resp.sendRedirect("error.jsp");
        }


    }

}
/*
 <servlet>
 <servlet-name>form</servlet-name>
 <servlet-class>cn.mldn.lxh.listener.CharServlet</servlet-class>
 <load-on-startup>0</load-on-startup>
 </servlet>
 <servlet-mapping>
 <servlet-name>form</servlet-name>
 <url-pattern>*.do</url-pattern>
 </servlet-mapping>
 */

